layui.use(['table', 'jquery','element','layer','form','upload','layedit'], function () {
    var table = layui.table;
    var $ = layui.jquery;
    var element = layui.element;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;
    var layedit = layui.layedit;
    layedit.build('demo');

    //第一个实例
    table.render({
        elem: '#table',
        height: 600,
        toolbar :'#toolbar',
        defaultToolbar : []
        , url: ctx + '/eng/sys/systemParam/listPage' //数据接口
        , page: true
        , cols: [[
            {field: 'name', title: '说明', align: 'center',width:''},
            {field: 'key', title: '参数标示', align: 'center',width:''},
            {field: 'value', title: '参数值', align: 'center',width:''},
            {field: 'ops', title: '操作', width: '', align: 'center',toolbar: '#tool'}
        ]]
    });


    // tool
    table.on('tool(table)', function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        if(layEvent === 'update') {
            if (data.type == "layedit") {
                //初始化数据
                $('#demo').html(data.value);
                var layIndex = layedit.build('demo',{
                    tool : [ 'strong' //加粗
                        ,'italic' //斜体
                        ,'underline' //下划线
                        ,'del' //删除线
                        ,'|' //分割线
                        ,'left' //左对齐
                        ,'center' //居中对齐
                        ,'right' //右对齐
                    ]
                }); //建立编辑器
                layer.open({
                    type :1,
                    content : $('#layeditLayer'),
                    area :['700px','460px'],
                    btn : ['确定'],
                    yes : function (index) {
                        $.ajax({
                            url : ctx + '/eng/sys/systemParam/update',
                            type : 'post',
                            data : {
                                id : data.id,
                                value : layedit.getContent(layIndex)
                            },
                            async : false,
                            success : function(res){
                                if(res.success){
                                    layer.closeAll();
                                    layer.msg(res.msg,{icon:1});
                                    table.reload('table');
                                }else{
                                    layer.msg(res.msg);
                                }
                            }
                        })
                    }
                })
            } else {
            layer.prompt({
                value: '',
                title: '请输入参数值',
            }, function (value, index, elem) {
                $.ajax({
                    url: ctx + "/eng/sys/systemParam/update",
                    data: {
                        id: data.id,
                        value: value
                    },
                    async: false,
                    success: function (res) {
                        if (res.success) {
                            table.reload('table');
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    }
                });

            });
        }
        }
    });
});